<?php

namespace App\Dao;

use App\Exceptions\SysExceptions;
use App\Model\MenuModel;
use App\Model\VipSettingsModel;
use Illuminate\Database\Eloquent\Builder;

class VipSettingsDao extends BaseDao
{
    /**
     * author:fhc
     * desc:获取菜单
     * created_at: {2020/10/16}----10:53 上午
     * @param array $params
     * @return Builder
     */
    public static function getVipSettingsBuilderQuery(array $params = []): Builder
    {
        $query = VipSettingsModel::query();

        $query->when(checkIsHasField($params,'vip_id'),function ($query)use ($params){
            $query->where('vip_id',$params['vip_id']);
        });

        return $query;
    }


    /**
     * Created by PhpStorm.
     * @purpose 添加vip
     * @Author: fhc
     * @Time: 2023/5/10 13:27
     * @throws SysExceptions
     */
    public static function add_vip($params = []){
        if(self::getVipSettingsBuilderQuery()->create($params) ===false){
            self::error('创建失败');
        }
    }


    /**
     * Created by PhpStorm.
     * @purpose 获取vip菜单
     * @Author: fhc
     * @Time: 2023/5/10 13:28
     */
    public static function get_vips_menu($params): \Illuminate\Contracts\Pagination\LengthAwarePaginator
    {
        return self::getVipSettingsBuilderQuery($params)->orderByDesc('created_at')->paginate();
    }

    /**
     * @throws SysExceptions
     */
    public static function edit_vip(array $params = [])
    {
        if(self::getVipSettingsBuilderQuery(['vip_id'=>self::deleteParamId($params,'vip_id')])->update(self::filterFields($params,new VipSettingsModel())) ===false){
            self::error('创建失败');
        }
    }

    /**
     * Created by PhpStorm.
     * @purpose
     * @Author: fhc
     * @Time: 2023/5/10 13:55
     */
    public static function get_vip_info($params = []){
        return self::getVipSettingsBuilderQuery($params)->first();
    }

    public static function del_vip(array $params)
    {
        if(self::getVipSettingsBuilderQuery(['vip_id'=>self::deleteParamId($params,'vip_id')])->delete() ===false){
            self::error('删除失败');
        }
    }

    public static function get_vips()
    {
        return self::getVipSettingsBuilderQuery()->orderBy('vip_index')->get();
    }


}
